﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using IL2SWAUtilities;

namespace IL2SWA
{
    public partial class PlayerDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {   
                DataTable resultsRegisteredPlayers = new DataTable();
                resultsRegisteredPlayers = IL2SWAUtilities.DataAccess.RunSP( "RepRegisteredPlayersGet", new string[] { }, new object[] { });
                RegisteredPlayers.DataSource = resultsRegisteredPlayers;
                RegisteredPlayers.DataTextField = resultsRegisteredPlayers.Columns[1].ToString();
                RegisteredPlayers.DataValueField  = resultsRegisteredPlayers.Columns[0].ToString();
                RegisteredPlayers.DataBind();
                SelectedPlayer.Text = "";
                SelectedPlayer.Visible = false;
                HumanAirKills.Text ="";
                HumanAirKills.Visible = false;
                AIAirKills.Text = "";
                AIAirKills.Visible = false;
                PlayerKilledByHumans.Text = "";
                PlayerKilledByHumans.Visible = false;
                PlayerKilledByAIs.Text = "";
                PlayerKilledByAIs.Visible =false;
                AliveStreak.Text = "";
                AliveStreak.Visible = false;

                if (Request.QueryString["PlayerName"] != null)
                {
                    RegisteredPlayers.SelectedIndex = RegisteredPlayers.Items.IndexOf(RegisteredPlayers.Items.FindByText(Request.QueryString["PlayerName"]));
                    RegisteredPlayers_SelectedIndexChanged(RegisteredPlayers, null);
                }
                
            }

            

        }

        protected void RegisteredPlayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            
            // PlayerHighLevelView
            SelectedPlayer.Text = "Game Data For " + RegisteredPlayers.SelectedItem.ToString() ;
            SelectedPlayer.Font.Bold = true;
            SelectedPlayer.Visible = true;
            DataTable resultsRegisteredPlayers = new DataTable();
            resultsRegisteredPlayers = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerTotalStatsGet", new string[] { "@PlayerID" }, new object[] { RegisteredPlayers.SelectedValue });
            PlayerHighLevelView.DataSource = resultsRegisteredPlayers;
            PlayerHighLevelView.DataBind();


            DataTable resultsAliveStreaks = new DataTable();
            resultsAliveStreaks = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerAliveStreaksGet", new string[] { "@PlayerID" }, new object[] { RegisteredPlayers.SelectedValue });
            if (resultsAliveStreaks != null)
            {
                AliveStreak.Text = "Best Alive Streak";
                AliveStreak.Visible = true;
            }
            AliveStreaks.DataSource = resultsAliveStreaks;
            AliveStreaks.DataBind();

            DataTable resultsPlayerHumanAirKills = new DataTable();
            resultsPlayerHumanAirKills = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerAirKills", new string[] { "@PlayerID", "AI" }, new object[] { RegisteredPlayers.SelectedValue, 0 });
            if (resultsPlayerHumanAirKills != null)
            {
                PlayerKilledByHumans.Text = "Player Shotdown By Humans";
                PlayerKilledByHumans.Visible = true;
            }
            PlayerHumanAirKills.DataSource = resultsPlayerHumanAirKills;
            PlayerHumanAirKills.DataBind();


            DataTable resultsPlayerKilledByHuman = new DataTable();
            resultsPlayerKilledByHuman = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerKilledByHumans", new string[] { "@PlayerID" }, new object[] { RegisteredPlayers.SelectedValue });
            if (resultsPlayerHumanAirKills != null)
            {
                HumanAirKills.Text = "Player Human AIR Kills";
                HumanAirKills.Visible = true;
            }
            PlayerKilledByHuman.DataSource = resultsPlayerKilledByHuman;
            PlayerKilledByHuman.DataBind();


            DataTable resultsPlayerKilledByAI = new DataTable();
            resultsPlayerKilledByAI = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerKilledByAI", new string[] { "@PlayerID" }, new object[] { RegisteredPlayers.SelectedValue });
            if (resultsPlayerKilledByAI != null)
            {
                PlayerKilledByAIs.Text = "Player Shotdown By AI/Crashed";
                PlayerKilledByAIs.Visible = true;
            }
            PlayerKilledByAI.DataSource = resultsPlayerKilledByAI;
            PlayerKilledByAI.DataBind();


            DataTable resultsPlayerAIAirKills = new DataTable();
            resultsPlayerAIAirKills = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerAirKills", new string[] { "@PlayerID", "AI" }, new object[] { RegisteredPlayers.SelectedValue, 1 });
            if (resultsPlayerAIAirKills != null)
            {
                AIAirKills.Text = "Player AI AIR Kills";
                AIAirKills.Visible = true;
            }
            PlayerAIAirKills.DataSource = resultsPlayerAIAirKills;
            PlayerAIAirKills.DataBind();

            DataTable resultsPlaneUseage = new DataTable();
            resultsPlaneUseage = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerPlaneUseage", new string[] { "@PlayerID" }, new object[] { RegisteredPlayers.SelectedValue });
            PlayerPlaneUseage.DataSource = resultsPlaneUseage;
            PlayerPlaneUseage.DataBind();
             
            DataTable resultsPlaneLoadoutFuel = new DataTable();
            resultsPlaneLoadoutFuel = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerPlaneLoadoutFuelUseage", new string[] { "@PlayerID" }, new object[] { RegisteredPlayers.SelectedValue });
            PlayerPlaneLoadoutFuel.DataSource = resultsPlaneLoadoutFuel;
            PlayerPlaneLoadoutFuel.DataBind();

            DataTable resultsPlayerTotalObjectKills = new DataTable();
            resultsPlayerTotalObjectKills = IL2SWAUtilities.DataAccess.RunSP( "RepPlayerTotalObjectKills", new string[] { "@PlayerID" }, new object[] { RegisteredPlayers.SelectedValue });
            PlayerObjectKills.DataSource = resultsPlayerTotalObjectKills;
            PlayerObjectKills.DataBind();

        }
    }
}